package com.tang.algorithm.hash;

import java.util.HashMap;

/**
 * @author tlm
 * @version 1.0
 * @date 2020/6/26 20:24
 */
public class Mst1602 {
    public static void main(String[] args) {
        String[] s = {"i", "have", "an", "apple", "he", "have", "a", "pen"};
        WordsFrequency wordsFrequency = new WordsFrequency(s);
        System.out.println(wordsFrequency.get("you"));
        System.out.println(wordsFrequency.get("have"));
        System.out.println(wordsFrequency.get("an"));
        System.out.println(wordsFrequency.get("apple"));
        System.out.println(wordsFrequency.get("pen"));


    }
}
class WordsFrequency {
    HashMap<String,Integer> hashMap = new HashMap<>();
    public WordsFrequency(String[] book) {
        for (int i = 0; i <book.length ; i++) {
            String key = book[i];

            if(hashMap.get(key)!=null){
                Integer value = hashMap.get(key);
                hashMap.put(key,++value);
            } else {
                hashMap.put(key,1);
            }
        }
    }

    public int get(String word) {
        if(hashMap.get(word)!=null){
            return hashMap.get(word);
        }
        else return 0;
    }
}
